// pagesLuxian/pages/guiHua/index.js
const app = getApp()

Page({
  data: {
    navData: {
      title: '路线规划',
      level: 2
    },
    navHeight: 32,
    navTop: 48,
    from: {
      address: null,
      local: null,
    },
    to: {
      address: null,
      local: null,
    },
    addressSearch: false,
    searchlist: [],
  },
  onLoad(options) {
    this.setData({
      navHeight: app.globalData.navHeight, //导航栏高度
      navTop: app.globalData.navTop, //导航栏距顶部距离
    });
    this.getlocal()
  },
  getlocal() {
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          ['from.local']: {
            longitude: res.longitude,
            latitude: res.latitude,
          }
        })
        that.getAddressByLocal(res.latitude, res.longitude)
      }
    })
  },
  fromAddressChg(e) {
    let value = e.detail.value;
    if (value) {
      this.setData({
        ['from.address']: value,
      })
      this.searchKeyAddress(value)
    }
  },
  searchKeyAddress(e) {
    if (!e) {
      this.setData({
        searchlist: []
      })
      return false;
    }
    var that = this;
    //调用关键词提示接口

    app.globalData.wxMapSdk.getSuggestion({
      keyword: e, //用户输入的关键词，可设置固定值,如keyword:'KFC'
      // region: '金华市', //设置城市名，限制关键词所示的地域范围，非必填参数
      // region_fix: 1,
      sig: app.globalData.mapSig,
      success: function (res) { //搜索成功后的回调
        if (res.status == 0) {
          // console.log(res.data)
          that.setData({
            addressSearch: true,
            searchlist: res.data
          })
        }
      },
    });
  },
  sureAddress(e) {
    // console.log(e)
    let address = e.currentTarget.dataset.address;
    let addressStr = address.city + address.district + address.title;
    this.getAddressByLocal(address.location.lat, address.location.lng, addressStr)
    // this.setData({
    //   from: {
    //     address: address.city + address.district + address.title,
    //     local: {
    //       longitude: address.location.lng,
    //       latitude: address.location.lat,
    //     }
    //   },
    //   addressSearch: false,
    //   searchlist: []
    // })
  },
  //逆地址解析
  getAddressByLocal(lat, lng, address) {
    // console.log(e)
    let that = this;
    app.globalData.wxMapSdk.reverseGeocoder({
      location: lat + ',' + lng,
      sig: app.globalData.mapSig,
      success: function (res) { //成功后的回调
        that.setData({
          addressSearch: false,
          searchlist: []
        })
        if (res.status == 0) {
          let result = res.result;
          let addressStr = address || result.address
          if (result.formatted_addresses && result.formatted_addresses.recommend) {
            addressStr = result.formatted_addresses.recommend
          }
          that.setData({
            ['from.address']: addressStr
          })
        } else if (address) {
          that.setData({
            ['from.address']: address
          })
        } else {
          wx.showToast({
            title: '获取位置信息失败',
          })
        }
      },
      error: function (err) {
        that.setData({
          addressSearch: false,
          searchlist: []
        })
        if (address) {
          that.setData({
            ['from.address']: address
          })
        } else {
          wx.showToast({
            title: '获取位置信息失败',
          })
        }
      }
    })
  },
  //地址聚焦
  addressFocus() {
    let query = wx.createSelectorQuery()
    query.select('.addressInt').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      // console.log(res)
      let top = res[0].top;
      let scrollTop = res[1].scrollTop;
      if (scrollTop != 0) {
        top = Number(top + scrollTop)
      }
      // 设置滚动条的位置
      wx.pageScrollTo({
        scrollTop: Number(top - 90)
      })
    })
    this.setData({
      addressSearch: true,
      searchlist: []
    })
  },
  //地址失焦
  addressBlur() {
    this.setData({
      addressSearch: false,
      searchlist: []
    })
  },
  findMore() {
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    prePage.setData({
      luxianNavIndex: 0
    })
    wx.navigateBack(-1)
  },
  toGetDest() {
    let { from, to } = this.data;
    let local = to.local || from.local;
    let query = '';
    if (local.longitude && local.latitude) {
      query = `?longitude=${local.longitude}&latitude=${local.latitude}`
    }
    wx.navigateTo({
      url: '/pagesLuxian/pages/dest/index' + query,
    })
  },
})